package com.whf.android.jar.util.date;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.net.URL;
import java.net.URLConnection;

public class NetTimeAsyncTask extends AsyncTask<String, Void, String> {

    private final IPostExecute mPostExecute;

    public NetTimeAsyncTask(IPostExecute execute) {
        this.mPostExecute = execute;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            final String alPaySign = params[0];
            if (TextUtils.isEmpty(alPaySign)) {
                return TimeUtils.getNowString();
            }
            URL url = new URL(alPaySign);
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            LogUtils.i("当前网络时间为:" + TimeUtils.millis2String(ld)
                    + "; 当前本地时间为:" + TimeUtils.getNowString());
            return TimeUtils.millis2String(ld);
        } catch (Exception e) {
            e.printStackTrace();
            return TimeUtils.getNowString();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (mPostExecute != null) {
            mPostExecute.onClick(result);
        }
    }

    public interface IPostExecute {
        void onClick(String result);
    }
    public interface IPostBooleanExecute {
        void onClick(boolean result);
    }
}
